package cn.edu.scau.cmi.huangzhiyuan.db.client;

import java.util.List;

import cn.edu.scau.cmi.huangzhiyuan.db.domain.Person;
import cn.edu.scau.cmi.huangzhiyuan.db.domain.Student;
import javafx.collections.ObservableList;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;

public class StudentPane extends BorderPane {

	public HeaderPane headerPane;
	public BodyPane bodyPane;
	public StudentFooterPane footerPane;

	public StudentPane() {
		setHeader();
		setBody();
		setFooter();
		setFooterVisible(false);
	}

	private void setHeader() {
		headerPane = new HeaderPane();
		setTop(headerPane);
	}

	private void setBody() {
		bodyPane = new BodyPane();
		setCenter(bodyPane);
	}

	private void setFooter() {
		footerPane = new StudentFooterPane();
		setBottom(footerPane);
	}
	
	public void setFooter(Student student, List<Person> teachers) {
		setFooterVisible(true);
		footerPane.setAttributes(student.getId(), student.getName(), student.getTeacher(), teachers);
	}
	
	public void setFooterVisible(Boolean canVisit) {
		getBottom().setVisible(canVisit);
	}
	
	public Button getFooterPaneDelBtn() {
		return footerPane.delBtn;
	}
	
	public Button getFooterPaneSaveBtn() {
		return footerPane.saveBtn;
	}
	
	public TextField getFooterPaneNameInput() {
		return footerPane.nameInput;
	}

	public ChoiceBox<Person> getFooterPaneTutors() {
		return footerPane.tutors;
	}
	
	public TextField getFooterPaneIdInput() {
		return footerPane.idInput;
	}
}





class StudentFooterPane extends HBox {
	
	public TextField idInput;
	public TextField nameInput;
	public ChoiceBox<Person> tutors;
	public Button saveBtn;
	public Button delBtn;

	public StudentFooterPane() {
		init();
	}
	
	private void init() {
		idInput = new TextField("123");
		nameInput = new TextField("张三");
		tutors = new ChoiceBox<>();
		saveBtn = new Button("保存");
		delBtn = new Button("删除");
		
		getChildren().addAll(idInput, nameInput, tutors, saveBtn, delBtn);
		
		setStyle();
	}
	
	private void setStyle() {
		idInput.setPrefWidth(100);
		nameInput.setPrefWidth(100);
		tutors.setPrefWidth(100);
		saveBtn.setPrefWidth(50);
		delBtn.setPrefWidth(50);

		idInput.setEditable(false);
	}
	
	
	public void setStudent(List<Person> teachers, Student student) {
		idInput.setText(String.valueOf(student.getId()));
		nameInput.setText(student.getName());
		
		int selectIndex = 0;
		ObservableList<Person> items = tutors.getItems();
		items.clear();
		for(int i=0; i<teachers.size(); i++) {
			if(items.get(i).equals(student.getTeacher().getName())) {
				selectIndex = i;
			}
			items.add(teachers.get(i));
		}
		tutors.getSelectionModel().select(selectIndex);
		
	}
	
	public void setAttributes(int id, String name, Person teacher, List<Person> teachers) {
		idInput.setText(String.valueOf(id));
		nameInput.setText(name);
		
		ObservableList<Person> items = tutors.getItems();
		int selectIndex = tutors.getSelectionModel().getSelectedIndex();
		items.clear();
		for(int i=0; i<teachers.size(); i++) {
			if(teacher!=null && teachers.get(i).getName().equals(teacher.getName())) {
				selectIndex = i;
			}
			
			items.add(teachers.get(i));
		}
		tutors.getSelectionModel().select(selectIndex);
	}
}